<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sales extends CI_Controller {
	function __construct()
	{
	parent::__construct();
		
		// check if salesman logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('sales/sales_model');
		$this->load->model('purchase/purchase_model');
		//$this->load->model('people/salesman_model');		
		$this->load->library('ion_auth');
		$this->load->library('session');
		
		$this->load->library('form_validation');
		$this->load->helper('url');
			$this->load->library('datatables');
		// Load MongoDB library instead of native db driver if required
		$this->config->item('use_mongodb', 'ion_auth') ?
		$this->load->library('mongo_db') :

		$this->load->database();

		$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));

	}
	function get(){
		echo $this->sales_model->get();
	}
		
	function add_sales(){
		
			if (!$this->ion_auth->logged_in())
			{
				redirect('auth', 'refresh');
			}

		$this->form_validation->set_rules('invoice_no', 'Invoice Number', 'required|xss_clean');
		$this->form_validation->set_rules('date', 'Bill date', 'required|xss_clean');
		//$this->form_validation->set_rules('id_customer', 'customer', 'required|xss_clean');
		
			
			$this->data['taxes']=$this->sales_model->get_tax();
			$this->data['customers']=$this->sales_model->get_customer();
			if($this->data['customers']==''){
				$this->data['customers']=array();
				}
			$id_branch=$this->session->userdata('id_branch');
			//echo $id_branch;exit;
			$meta['page_title'] = "Add Sales";
	   		$branch=$this->sales_model->get_invoicecode($id_branch)->row_array();
			$profile=$this->sales_model->get_profile()->row_array();
			$rate=$this->sales_model->get_invoiceno()->row_array();
			$r_n=$this->sales_model->get_invoiceno()->num_rows();
			$invoice=$this->sales_model->get_invoiceid()->row_array();
			if($r_n == 0){	
			$this->data['ic'] = $profile['invoice_code']."/".$branch['code']."/"."1";	
				}else{
					$new_id = $invoice['id']+1;
					$this->data['ic'] =$profile['invoice_code']."/".$branch['code']."/".$new_id;
					
				}
			
		if (isset($_POST) && !empty($_POST))
		{
			// do we have a valid request?
			/*if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
			{
				show_error('This form post did not pass our security checks.');
			}*/

//			echo "i am here";exit;
			
			if($this->input->post('trans_name')!=''){
				
				$trans_name = $this->input->post('trans_name');
				}else{
					$trans_name ='';
				}
				
			
			if($this->input->post('trans_amt')!=''){
				
				$trans_amt = $this->input->post('trans_amt');
				}else{
					$trans_amt =0;
				}
			$id_customer = $this->input->post('id_customer');
			if($id_customer!='0'){
				$data = array(
				'invoice_no' => $this->input->post('invoice_no'),
				'invoice_type' => $this->input->post('invoice_type'),
				'id_customer' => $id_customer,
				'customer_name' => strtoupper($this->sales_model->get_cname($id_customer)),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
				'round_off' => $this->input->post('round_off'),
				'total_price' => $this->input->post('total_price'),
				'transaction_type' => $this->input->post('type'),
				'date' => $this->input->post('date'),
				'transport_name' => strtoupper($trans_name),	
				'transport_amt' => $trans_amt,
				'final_price' => $this->input->post('total_price'),
				'id_branch' => 	$id_branch,
				'billed_by' => 	strtoupper($this->session->userdata('username')	)
			
				);	
				//print_r($data);exit;
				//print_r($data);exit;
			}else{
				$data = array(
				'invoice_no' => $this->input->post('invoice_no'),
				'invoice_type' => $this->input->post('invoice_type'),
				'id_customer' => $this->input->post('id_customer'),
				'customer_name' =>strtoupper( $this->sales_model->get_cname($this->input->post('id_customer'))),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
				'round_off' => $this->input->post('round_off'),
				'total_price' => $this->input->post('total_price'),
				'transaction_type' => $this->input->post('type'),
				'date' => $this->input->post('date'),
				'transport_name' => strtoupper($trans_name),	
				'transport_amt' => $trans_amt,
				'final_price' => $this->input->post('total_price'),
				'id_branch' => 	$id_branch,
				'billed_by' => strtoupper($this->session->userdata('username'))
			);
				
				
			} 
			//print_r($data);exit;
			if($data['transaction_type']==0){
				
				$data['status']=1;
			}
			//print_r($user->id);exit;
			//Update the groups user belongs to
			if ($this->form_validation->run() === TRUE)
			{
				//print_r($data);exit;
				$latest_id=$this->sales_model->insert($data);
				//echo $latest_id;exit;
				$name = $this->input->post('name');
				$code = $this->input->post('code');
				$rate = $this->input->post('rate');
				$quantity = $this->input->post('quantity');
				$amount = $this->input->post('amount');
				$discount = $this->input->post('discount');
				$tax = $this->input->post('tax');
				$total = $this->input->post('total');
				//print_r($name);exit;
				$length = count($code);
					for($i=0; $i<$length; $i++){
						$new_data['date'] = $data['date'];
						$new_data['name'] = $name[$i];
						$new_data['id_sale'] = $latest_id;
						$new_data['id_product'] =$this->sales_model->getID($code[$i]);
						$new_data['quantity'] = $quantity[$i];
						$new_data['rate'] = $rate[$i];
						$new_data['amount'] = $amount[$i];
						$new_data['tax'] = $tax[$i];	
						$new_data['discount'] = $discount[$i];
						$new_data['total'] = $total[$i];
						$new_data['ip']  =  $_SERVER['REMOTE_ADDR'];	
						//print_r($new_data);exit;
						//echo $id_branch;exit;
						$this->sales_model->insert_item($new_data,$id_branch);	
					}							
				//echo "welldone prabhas!!";exit;
				redirect("sales/sales/view_sales");
			
				
				
			}
		}

		//display the edit user form
		//$this->data['csrf'] = $this->_get_csrf_nonce();

		//set the flash data error message if there is one
		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		
 		$in_v = $this->data['ic'];
		$this->data['invoice_no'] = array(
			'name'  => 'invoice_no',
			'id'    => 'invoice_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('invoice_no',$in_v),
		);

		$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('date'),
			'class' => 'datepicker'
		);

	$this->data['id_customer'] = array(
			'name'  => 'id_customer',
			'id'    => 'id_customer',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_customer'),
		);
		
			$this->data['taxes']=$this->sales_model->get_tax();
			$this->load->view('common/header', $meta);
			$this->load->view('sales/add_sales', $this->data);
			$this->load->view('common/footer');
	   
	}
	
	
	function view_sales(){
	   $this->data['users'] = $this->sales_model->get_salesList()->result();
		//print_r($this->data['users']);exit;
	   $meta['page_title'] = "Sales";
	   $this->data['message'] = "Sales";
	   $this->load->view('common/header', $meta);
	   $this->load->view('sales/view_sales1');
       $this->load->view('common/footer');	
	
}

function view_unique_sales($id, $cod1, $cod2 ,$cod3){
$meta['title']="Sales Detail";
$this->data['referance_no']=$cod1.'/'.$cod2.'/'.$cod3;
$this->data['users']= $this->sales_model->get_uniquepurchase($id)->result();
$this->load->view('purchase/view_unique_purchase',$this->data);

}

	
		
	function get_product(){
		$id_branch=$this->session->userdata('id_branch');
	 $rate=$this->sales_model->get_product($_REQUEST['code'],$id_branch);
	echo $rate;exit; 
	
	}
	
	
	function search_quantity(){
		$id_branch=$this->session->userdata('id_branch');
		$rate=$this->sales_model->search_quantity($_REQUEST['code'], $_REQUEST['qty'],$id_branch);
		echo $rate;exit;
		
	}
	
function delete_sale($id){
$this->sales_model->delete($id);
redirect("sales/sales/view_sales");

}	
	
	
function report($id){
	$meta['page_title'] = 'Sales Report';
	$sales_man=$this->session->userdata('username');
	$this->data['sales_man']= $sales_man;
	$sales = $this->sales_model->get_sales($id)->row_array();
	$this->data['sales']= $sales;
	//print_r($sales);exit;
	$this->data['profile'] = $this->sales_model->get_company_profile()->row_array();
	$customer = $this->sales_model->get_customer_details($sales['id_customer'])->row_array();
	if(count($customer)> 0){
			$this->data['customer']= $customer;
			$this->data['invoice_no']=$sales['invoice_type'];
			$this->data['referance_no']=$sales['invoice_no'];
			$this->data['products']= $this->sales_model->get_uniquesale($id)->result();
			//print_r($this->data);exit;
			$meta['title']='Sales Report';
			$this->load->view('common/header', $meta);
		   $this->load->view('sales/sale_report',$this->data);
	       $this->load->view('common/footer');	
	}else{
		
			$this->data['invoice_no']=$sales['invoice_type'];
			$this->data['referance_no']=$sales['invoice_no'];
			$this->data['products']= $this->sales_model->get_uniquesale($id)->result();
			$meta['title']='Sales Report';
			$this->load->view('common/header', $meta);
		   	$this->load->view('sales/sale_report1',$this->data);
	       	$this->load->view('common/footer');	

		
	}
}	


function search_item(){
			
			
			$meta['page_title'] = 'Item Search';
			$this->load->view('common/header', $meta);
		   	$this->load->view('sales/search_item');
	       	$this->load->view('common/footer');	
	
}


	
function edit_sales($id){
		if (!$this->ion_auth->logged_in())
			{
				redirect('auth', 'refresh');
			}
			
		$user = $this->sales_model->edit_sales($id)->row();
	//print_r($user);exit;
		$products = $this->sales_model->edit_products($id)->result();
		$up_sales = $this->sales_model->edit_sales($id)->row_array();
		$up_products = $this->sales_model->edit_products($id)->result_array();
		//print_r("<pre>");
		//print_r($up_products);exit;
		$this->form_validation->set_rules('invoice_no', 'Invoice Number', 'required|xss_clean');
		$this->form_validation->set_rules('date', 'date', 'required|xss_clean');
		//$this->form_validation->set_rules('id_supplier', 'Supplier', 'required|xss_clean');
			$this->data['taxes']=$this->sales_model->get_tax();
			$this->data['customers']=$this->sales_model->get_customer();
//			print_r($this->data['customers']);exit;
			//$this->data['branches']=$this->purchase_model->get_branch();
			
			if($this->data['taxes']==''){
				$this->data['taxes']=array();
				}
			if($this->data['customers']==''){
				$this->data['customers']=array();
				}
			$id_branch=$this->session->userdata('id_branch');
			$meta['page_title'] = "Edit Sales";
	   		$branch=$this->sales_model->get_invoicecode($id_branch)->row_array();
			$profile=$this->sales_model->get_profile()->row_array();
		if (isset($_POST) && !empty($_POST))
		{
			// do we have a valid request?
			/*if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
			{
				show_error('This form post did not pass our security checks.');
			}*/

//			echo "i am here";exit;
			
			if($this->input->post('trans_name')!=''){
				
				$trans_name = $this->input->post('trans_name');
				}else{
					$trans_name ='';
				}
				
			
			if($this->input->post('trans_amt')!=''){
				
				$trans_amt = $this->input->post('trans_amt');
				}else{
					$trans_amt =0;
				}
			
			if($this->input->post('id_branch')!=0){
				$id_branch = $this->input->post('id_branch');
				}else{
					
					$id_branch=0;
				}
		
			$tx = $this->input->post('tx');
			$id_customer = $this->input->post('id_customer');
			if($id_customer!='0'){
				$data = array(
				'invoice_no' => strtoupper($this->input->post('invoice_no')),
				'invoice_type' => $this->input->post('invoice_type'),
				'id_customer' => $id_customer,
				'customer_name' => strtoupper($this->sales_model->get_cname($id_customer)),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
			
				'round_off' => $this->input->post('round_off'),
				'total_price' => $this->input->post('total_price'),
				'transaction_type' => $this->input->post('type'),
				'date' => $this->input->post('date'),
				'transport_name' => strtoupper($trans_name),	
				'transport_amt' => $trans_amt,
				'final_price' => $this->input->post('total_price'),
				'id_branch' => 	$up_sales['id_branch'],
				'billed_by' => 	$this->session->userdata('username')	
			
				);	
				//print_r($data);exit;
				//print_r($data);exit;
			}else{
				$data = array(
				'invoice_no' => strtoupper($this->input->post('invoice_no')),
				'invoice_type' => $this->input->post('invoice_type'),
				'id_customer' => $this->input->post('id_customer'),
				'customer_name' => strtoupper($this->sales_model->get_cname($this->input->post('id_customer'))),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
				
				'round_off' => $this->input->post('round_off'),
				'total_price' => $this->input->post('total_price'),
				'transaction_type' => $this->input->post('type'),
				'date' => $this->input->post('date'),
				'transport_name' =>strtoupper( $trans_name),	
				'transport_amt' => $trans_amt,
				'final_price' => $this->input->post('total_price'),
				'id_branch' =>	$up_sales['id_branch'],
				'billed_by' => 	$this->session->userdata('username')
			);
			}
			
		//	print_r($data);exit;
			//print_r($user->id);exit;
			//Update the groups user belongs to
			if ($this->form_validation->run() === TRUE)
			{
				//echo "i am here";exit;
				//$this->sales_model->update_sale($data,$id);
				$purchase_product_id = $this->input->post('purchase_product_id');
				
				$name = $this->input->post('name');
				
				$code = $this->input->post('code');
				$rate = $this->input->post('rate');
				$quantity = $this->input->post('quantity');
				$prev_quantity = $this->input->post('prev_quantity');
				$amount = $this->input->post('amount');
				$discount = $this->input->post('discount');
				$tax = $this->input->post('tax');
				$total = $this->input->post('total');
				$length = count($code);
					for($i=0; $i<$length; $i++){
						$id_pur_product = $purchase_product_id[$i];
						$new_data['id_sale'] = $id;
						$new_data['name'] = $name[$i];
						$new_data['date'] = $data['date'];
						$new_data['id_product'] = $this->purchase_model->get_product_id($code[$i]);
						$new_data['quantity'] = $quantity[$i];
						$new_data['rate'] = $rate[$i];
						$new_data['amount'] = $amount[$i];
						$new_data['tax'] = $tax[$i];	
						$new_data['discount'] = $discount[$i];
						$new_data['total'] = $total[$i];
						$new_data['ip']  =  $_SERVER['REMOTE_ADDR'];
						$previous_quantity = $prev_quantity[$i];
						$this->sales_model->update_item($new_data,$up_sales['id_branch'],$id_pur_product,$previous_quantity);	
                 
						}				
									
				//echo "welldone prabhas!!";exit;
				redirect("sales/sales/view_sales");
			}
		}

		//display the edit user form
		//$this->data['csrf'] = $this->_get_csrf_nonce();

		//set the flash data error message if there is one
		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
		
		
 		$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('date',$user->date),
			
		);
			
		
		$this->data['invoice_no'] = array(
			'name'  => 'invoice_no',
			'id'    => 'invoice_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('invoice_no',$user->invoice_no),
		);
	
			$this->data['products'] = $products;
			$this->data['user'] = $user;
			$this->data['taxes']=$this->sales_model->get_tax();
			//print_r($products);exit;
			$this->load->view('common/header', $meta);
			$this->load->view('sales/edit_sales', $this->data);
			$this->load->view('common/footer');
	
}	
	
	

function  cancel_sales(){
	$meta['page_title'] = "Cancel Sales";
	$this->data['customers']=$this->sales_model->get_customer();
	$this->data['invoices']=$this->sales_model->get_invoices();
	$this->data['cust_invoices']=$this->sales_model->get_custinvoices();
	
		$this->load->view('common/header', $meta);
		$this->load->view('sales/cancel_sales',$this->data);
	  	$this->load->view('common/footer');	
}	

function get_sales(){
	$query1 = $this->sales_model->edit_sales($_REQUEST['id_sales']);
	$query2 = $this->sales_model->edit_products($_REQUEST['id_sales']);
	
		$id_branch=$this->session->userdata('id_branch');
			//echo $id_branch;exit;
			$meta['page_title'] = "Add Sales";
	   		$branch=$this->sales_model->get_invoicecode($id_branch)->row_array();
			$profile=$this->sales_model->get_profile()->row_array();
			$rate=$this->sales_model->get_invoiceno()->row_array();
			$r_n=$this->sales_model->get_invoiceno()->num_rows();
			$invoice=$this->sales_model->get_invoiceid()->row_array();
			if($r_n == 0){	
			$this->data['ic'] = $profile['invoice_code']."/".$branch['code']."/"."1";	
				}else{
					$new_id = $invoice['id']+1;
					$this->data['ic'] =$profile['invoice_code']."/".$branch['code']."/".$new_id;
					
			}
		
	
	$user = $query1->row();
	$products = $query2->result();
	$old_user = $query1->row_array();
	$old_products = $query2->result_array();
	$this->data['taxes']=$this->sales_model->get_tax();
			$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('date',$user->date),
			
		);
		
		$this->data['id_customer'] = array(
			'name'  => 'id_customer',
			'id'    => 'id_customer',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_customer',$user->id_customer),
		);
			
		$this->data['cancel_date'] = array(
			'name'  => 'cancel_date',
			'id'    => 'cancel_date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('cancel_date'),
			'class' => 'datepicker'
		);

		$in_v = $this->data['ic'];
		$this->data['invoice_no'] = array(
			'name'  => 'invoice_no',
			'id'    => 'invoice_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('invoice_no',$in_v),
		);
	
		
	
	
	$this->data['products'] = $products;
	$this->data['user'] = $user;
	$this->load->view('sales/cancel_part_sales',$this->data);
	
	
}


function get_invoiceno(){

	$invoices=$this->sales_model->cancel_invoiceno($_REQUEST['id_user']);
	echo  $invoices;exit;	
	}

	
}

	
	

?>